﻿ using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QLNhaSach.BUS;
using QLNhaSach.BUS.DTO;

namespace QLNhaSach.GUI.NhanVienKeToan
{
    public partial class Xem_Hóa_Đơn_Khách_Hàng : Form
    {
        DataTable dt = new DataTable();
        public Xem_Hóa_Đơn_Khách_Hàng()
        {
            InitializeComponent();
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Xem_Hóa_Đơn_Khách_Hàng_Load(object sender, EventArgs e)
        {
            dt = NhanVienKeToanBUS.DSHD();
            lvDSHoaDonKH.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                ListViewItem item = new ListViewItem();
                item.Text = dr["NgayCap"].ToString();
                item.SubItems.Add(dr["HoTen"].ToString());
                item.SubItems.Add(dr["kh.MaKH"].ToString());
                item.SubItems.Add(dr["TenKH"].ToString());
                item.SubItems.Add(dr["Loai"].ToString());
                lvDSHoaDonKH.Items.Add(item);
            }
        }

        private void btnXemHoaDon_Click(object sender, EventArgs e)
        {
            if (txtTenKH.Text == "")
            {
                MessageBox.Show("Tên khách hàng không được để trống");
                txtTenKH.Focus();
                return;
            }
            //hd.MaHD, hd.NgayCap, kh.TenKH, nv.HoTen
            string sql = "SELECT * FROM ((HoaDon hd INNER JOIN ";
            sql += "KhachHang kh ON hd.MaKH = kh.MaKH) INNER JOIN NhanVien nv ON hd.MaNV = nv.MaNV) WHERE ";
            sql += " (kh.TenKH LIKE '%" + txtTenKH.Text + "%') ";
            if (txtTenNV.Text != "")
                sql += " AND (nv.HoTen LIKE '%" + txtTenNV.Text + "%') ";
            if (txtMaKH.Text != "")
            {
                long temp;
                if (long.TryParse(txtMaKH.Text, out temp) == false)
                {
                    MessageBox.Show("Mã khách hàng là ký số");
                    txtMaKH.Text = "";
                    txtMaKH.Focus();
                    return;
                }
                sql += " OR (kh.MaKH = " + Convert.ToInt16(txtMaKH.Text) + ")";
            }
            if (rbNhap.Checked == true)
                sql += " AND (hd.Loai = 1)";
            else
                sql += " AND (hd.Loai = 0)";
            sql += " OR (hd.NgayCap = #" + dtpNgayLap.Text + "#) ";
            dt = NhanVienKeToanBUS.TimHoaDon(sql);
            lvDSHoaDonKH.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                ListViewItem item = new ListViewItem();
                item.Text = dr["NgayCap"].ToString();
                item.SubItems.Add(dr["HoTen"].ToString());
                item.SubItems.Add(dr["hd.MaKH"].ToString());
                item.SubItems.Add(dr["TenKH"].ToString());
                item.SubItems.Add(dr["Loai"].ToString());
                lvDSHoaDonKH.Items.Add(item);
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            txtMaKH.Text = "";
            txtTenKH.Text = "";
            txtTenNV.Text = "";
            dtpNgayLap.Text = DateTime.Now.ToString();
            txtTenNV.Focus();
            Xem_Hóa_Đơn_Khách_Hàng_Load(sender, e);
        }
    }
}